package com.kfm.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.hotel.pojo.SysUser;
import com.kfm.hotel.service.SysUserService;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("system/user/index");

        return mv;
    }

    @PostMapping
    public R index(SysUser sysUser){
        System.out.println(sysUser);
        int row = sysUserService.addUser(sysUser);
        if (row > 0){
            // 新增成功
            return R.ok();
        } else{
            // 新增失败
            return R.failed("数据库操作失败");
        }
    }

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNumber
                            , @RequestParam(defaultValue = "5")Integer pageSize
                            , SysUser sysUser){
        return R.ok(sysUserService.list(pageNumber, pageSize, sysUser));
    }

    @GetMapping("/list2")
    public List<SysUser> list2(@RequestParam(defaultValue = "1") Integer pageNumber
            , @RequestParam(defaultValue = "5")Integer pageSize
            , SysUser sysUser){
        return sysUserService.list(pageNumber, pageSize, sysUser).getList();
    }
}
